package api

import (
	"api-gateway/basic/inits"
	order "api-gateway/basic/order_proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// AddressCreate
// @Summary      收货地址添加
// @Description  收货地址添加
// @Tags         物流模块
// @Accept       json
// @Produce      json
// @Param        address  body      request.AddressCreate  true  "物流参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /address/addressCreate [post]
func AddressCreate(c *gin.Context) {
	var v request.AddressCreate
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	create, err := inits.OrderClient.AddressCreate(c, &order.AddressCreateReq{
		UserId:        int64(v.UserId),
		RecipientName: v.RecipientName,
		Mobile:        v.Mobile,
		Province:      v.Province,
		City:          v.City,
		District:      v.District,
		DetailAddress: v.DetailAddress,
		ReceiverZip:   v.ReceiverZip,
	})
	if err != nil {
		response.Error(c, 400, "收货地址添加失败", err.Error())
		return
	}
	response.Success(c, "收货地址添加成功", create)
}

// AddressList
// @Summary      收货地址列表
// @Description  收货地址列表
// @Tags         物流模块
// @Accept       json
// @Produce      json
// @Param        address  query    request.AddressList  true  "物流参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /address/addressList [get]
func AddressList(c *gin.Context) {
	var v request.AddressList
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	list, err := inits.OrderClient.AddressList(c, &order.AddressListReq{
		UserId: int64(v.UserId),
	})
	if err != nil {
		response.Error(c, 400, "收货地址列表失败", err.Error())
		return
	}
	response.Success(c, "收货地址列表展示成功", list)
}

// AddressDel
// @Summary      收货地址删除
// @Description  收货地址删除
// @Tags         物流模块
// @Accept       json
// @Produce      json
// @Param        address  body    request.AddressDel  true  "物流参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /address/addressDel [post]
func AddressDel(c *gin.Context) {
	var v request.AddressDel
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	create, err := inits.OrderClient.AddressDel(c, &order.AddressDelReq{
		Id: int64(v.Id),
	})
	if err != nil {
		response.Error(c, 400, "收货地址删除失败", err.Error())
		return
	}
	response.Success(c, "收货地址删除成功", create)
}

// RealTimeExpress
// @Summary      实时物流查询
// @Description  实时物流查询
// @Tags         物流模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.RealTimeExpress  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /address/RealTimeExpress [post]
func RealTimeExpress(c *gin.Context) {
	var v request.RealTimeExpress
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	retail, err := inits.OrderClient.RealTimeExpress(c, &order.RealTimeExpressReq{
		Company:  v.Company,
		Tracking: v.Tracking,
	})
	if err != nil {
		response.Error(c, 400, "实时物流查询失败", err.Error())
		return
	}
	response.Success(c, "实时物流查询成功", retail)
}

// CreateSales
// @Summary      创建售后
// @Description  创建售后
// @Tags         物流模块
// @Accept      json
// @Produce     json
// @Param        address  body   request.CreateSales  true  "物流参数"
// @Success      200  {object}   response.Response
// @Failure      400   {object}  response.Response
// @Router       /address/createSales [post]
func CreateSales(c *gin.Context) {
	//userId := c.GetUint("userId")
	var req request.CreateSales
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	sales, err := inits.OrderClient.CreateSales(c, &order.CreateSalesReq{
		UserId:      int64(req.UserId),
		OrderId:     int64(req.OrderId),
		Cause:       req.Cause,
		SalesImg:    req.SalesImg,
		Status:      int64(req.Status),
		SalesPrice:  float32(req.SalesPrice),
		SalesStatus: int64(req.SalesSatatus),
	})
	if err != nil {
		response.Error(c, 400, "售后退款失败", err.Error())
		return
	}
	response.Success(c, "售后退款成功", sales)
}
